/*
 * ServiceUtils.fx
 *
 * Created on Feb 27, 2010, 11:14:18 PM
 */
package com.gpsbuddy.utils;

import javafx.io.http.HttpRequest;
import com.gpsbuddy.services.User;
import com.gpsbuddy.services.UsersParser;
import java.lang.Exception;
import com.gpsbuddy.main.Enviroment;
import java.io.OutputStream;
import java.io.IOException;
import javafx.io.http.HttpHeader;
import com.gpsbuddy.navigation.FriendsPage;
import com.gpsbuddy.services.FriendRequest;

/**
 * @author jonycus
 */
public function loginToService(username: String, password: String): Void {
    var users: User[];
    var uname = username;
    var pass = password;
    if (uname == null) {
        if (DatabaseUtils.hasProp("username")) {
            uname = DatabaseUtils.getPropValue("username");
            }
        }
    if (pass == null) {
        if (DatabaseUtils.hasProp("password")) {
            pass = DatabaseUtils.getPropValue("password");
            }
        }
    var request: HttpRequest = HttpRequest {
                location: "http://jonycus.dyndns.org:8084/TestRestService1/resources/uup?username={uname}&password={pass}";
                method: HttpRequest.GET;
                onException: function (exception: Exception) {
                    exception.printStackTrace();
                    }
                onResponseCode: function (responseCode: Integer) {
                    if (responseCode != 200) {
                        println("Web service called failed. Response code was: {responseCode} with {request.responseMessage}");
                        } else {
                        println("Web service called succeded!!!");
                        }
                    }
                onInput: function (input: java.io.InputStream) {

                    try {
                        var parser = UsersParser { };
                        users = parser.parse(input);
                        if (parser.errorMessage.length() > 0) {
                            println(parser.errorMessage);
                            }

                        } finally {
                        input.close();
                        }
                    }
                onDone: function () {
                    if (sizeof users > 0) {
                        Enviroment.statusText = "Connected!!!";
                        Enviroment.connected = true;
                        Enviroment.loggedUser = users[0];
                    } else {
                        Enviroment.statusText = "Error connecting...";
                        Enviroment.connected = false;
                    }

                    }
            }
    request.start();
    }

public function registerToService(user: User): Void {
    var userContent = user.toXML().getBytes();
    var request: HttpRequest = HttpRequest {
                location: "http://jonycus.dyndns.org:8084/TestRestService1/resources/uup/";
                method: HttpRequest.PUT;
                headers: [
                    HttpHeader {
                        name: HttpHeader.CONTENT_TYPE;
                        value: "application/xml";
                    },
                    HttpHeader {
                        name: HttpHeader.CONTENT_LENGTH;
                        value: "{userContent.length}";
                    }
                ];
                onException: function (exception: Exception) {
                    exception.printStackTrace();
                    }
                onOutput: function (os: OutputStream) {
                    try {
                        println("onOutput - about to write {userContent.length} bytes to output stream");
                        os.write(userContent);
                    } catch (ex: IOException) {
                        ex.printStackTrace();
                    } finally {
                        os.close();
                    }

                    }
                onResponseCode: function (responseCode: Integer) {
                    println("responeCode: {responseCode}");
                    if (responseCode != 201) {
                        println("Web service called failed. Response code was: {responseCode} with {request.responseMessage}");
                        } else {
                        println("Web service called succeded!!!");
                        }
                    }
                onDoneRead: function () {
                    println("onDoneRead")
                }
                onDone: function () {
                    println("onDone")
                }
            }
    request.start();
    }

public function findFriends(name: String, surname: String) {
    var foundUsers: User[];
    var request: HttpRequest = HttpRequest {
                location: "http://jonycus.dyndns.org:8084/TestRestService1/resources/friends?name={name}&surname={surname}"
                onException: function (exception: Exception) {
                    exception.printStackTrace();
                    }
                onResponseCode: function (responseCode: Integer) {
                    if (responseCode != 200) {
                        println("Web service called failed. Response code was: {responseCode} with {request.responseMessage}");
                        } else {
                        println("Web service called succeded!!!");
                        }
                    }
                onInput: function (input: java.io.InputStream) {

                    try {
                        var parser = UsersParser { };
                        foundUsers = parser.parse(input);
                        if (parser.errorMessage.length() > 0) {
                            println(parser.errorMessage);
                            }

                        } finally {
                        input.close();
                        }
                    }
                onDone: function () {
                    FriendsPage.foundUsers = foundUsers;
                    }
            }
    request.start();
    }

public function makeFriendRequest(rUser: User, tUser: User) {
    var friendRequest = FriendRequest {
                rUser: rUser
                tUser: tUser
            }
    var freiendRequestContent = friendRequest.toXML().getBytes();
    var request: HttpRequest = HttpRequest {
                location: "http://jonycus.dyndns.org:8084/TestRestService1/resources/friends/requests/";
                method: HttpRequest.PUT;
                headers: [
                    HttpHeader {
                        name: HttpHeader.CONTENT_TYPE;
                        value: "application/xml";
                    },
                    HttpHeader {
                        name: HttpHeader.CONTENT_LENGTH;
                        value: "{freiendRequestContent.length}";
                    }
                ];
                onException: function (exception: Exception) {
                    exception.printStackTrace();
                    }
                onOutput: function (os: OutputStream) {
                    try {
                        println("onOutput - about to write {freiendRequestContent.length} bytes to output stream");
                        os.write(freiendRequestContent);
                    } catch (ex: IOException) {
                        ex.printStackTrace();
                    } finally {
                        os.close();
                    }

                    }
                onResponseCode: function (responseCode: Integer) {
                    println("responeCode: {responseCode}");
                    if (responseCode != 201) {
                        println("Web service called failed. Response code was: {responseCode} with {request.responseMessage}");
                        } else {
                        println("Web service called succeded!!!");
                        }
                    }
                onDoneRead: function () {
                    println("onDoneRead")
                }
                onDone: function () {
                    println("onDone")
                }
            }
    request.start();
    }

public function getFriendRequests(rUser: User) {
    var id = rUser.id;
    var foundRequests: User[];
    var request: HttpRequest = HttpRequest {
                location: "http://jonycus.dyndns.org:8084/TestRestService1/resources/friends/requests?userId={id}";
                method: HttpRequest.GET;
                onException: function (exception: Exception) {
                    exception.printStackTrace();
                    }
                onResponseCode: function (responseCode: Integer) {
                    if (responseCode != 200) {
                        println("Web service called failed. Response code was: {responseCode} with {request.responseMessage}");
                        } else {
                        println("Web service called succeded!!!");
                        }
                    }
                onInput: function (input: java.io.InputStream) {
                    try {
                        var parser = UsersParser { };
                        foundRequests = parser.parse(input);
                        if (parser.errorMessage.length() > 0) {
                            println(parser.errorMessage);
                            }

                        } finally {
                        input.close();
                        }
                    }
                onDone: function () {
                    FriendsPage.foundRequests = foundRequests;
                    }
            }
    request.start();
    }

public function acceptFriendRequest(rUser: User, tUser: User) {
        var friendRequest = FriendRequest {
                rUser: rUser
                tUser: tUser
            }
    var freiendRequestContent = friendRequest.toXML().getBytes();
    var request: HttpRequest = HttpRequest {
                location: "http://jonycus.dyndns.org:8084/TestRestService1/resources/friends/approve/";
                method: HttpRequest.PUT;
                headers: [
                    HttpHeader {
                        name: HttpHeader.CONTENT_TYPE;
                        value: "application/xml";
                    },
                    HttpHeader {
                        name: HttpHeader.CONTENT_LENGTH;
                        value: "{freiendRequestContent.length}";
                    }
                ];
                onException: function (exception: Exception) {
                    exception.printStackTrace();
                    }
                onOutput: function (os: OutputStream) {
                    try {
                        println("onOutput - about to write {freiendRequestContent.length} bytes to output stream");
                        os.write(freiendRequestContent);
                    } catch (ex: IOException) {
                        ex.printStackTrace();
                    } finally {
                        os.close();
                    }

                    }
                onResponseCode: function (responseCode: Integer) {
                    println("responeCode: {responseCode}");
                    if (responseCode != 201) {
                        println("Web service called failed. Response code was: {responseCode} with {request.responseMessage}");
                        } else {
                        println("Web service called succeded!!!");
                        }
                    }
                onDoneRead: function () {
                    println("onDoneRead")
                }
                onDone: function () {
                    println("onDone")
                }
            }
    request.start();
}

public function getFriends(rUser: User) {
    var id = rUser.id;
    var foundFriends: User[];
    var request: HttpRequest = HttpRequest {
                location: "http://jonycus.dyndns.org:8084/TestRestService1/resources/friends/friends?userId={id}";
                method: HttpRequest.GET;
                onException: function (exception: Exception) {
                    exception.printStackTrace();
                    }
                onResponseCode: function (responseCode: Integer) {
                    if (responseCode != 200) {
                        println("Web service called failed. Response code was: {responseCode} with {request.responseMessage}");
                        } else {
                        println("Web service called succeded!!!");
                        }
                    }
                onInput: function (input: java.io.InputStream) {
                    try {
                        var parser = UsersParser { };
                        foundFriends = parser.parse(input);
                        if (parser.errorMessage.length() > 0) {
                            println(parser.errorMessage);
                            }

                        } finally {
                        input.close();
                        }
                    }
                onDone: function () {
                    FriendsPage.foundFriends = foundFriends;
                    }
            }
    request.start();
    }


